package tp.pr4;

import java.awt.EventQueue;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import tp.pr4.cityLoader.CityLoaderFromTxtFile;
import tp.pr4.gui.*;
/**
 * Application entry-point. The app admits a parameter with the name of the map file to be used.
 * If no arg is specified (or more than one file is given), an error message is printed (in System.err) 
 * and the application finishes with an error code (-1). If the map file cannot be read (or it does not exist), 
 * the app ends with a distinct error code (-2). Otherwise, the game is played and eventually the application 
 * will end normally (return code 0).
 * @author Alvaro Saez Hernando
 * @author Noel Clemente Montero 
 */
public class Main {
	
	/**
	 * Application entry-point
	 * @param args
	 */
	public static void main(String[] args){
		
		CityLoaderFromTxtFile fileLoader= null;
		City city = null;
		String map = null;
		
		//Options
		Options options = new Options();	
		options.addOption("h","help",false,"Shows this help messege");
		options.addOption("i","interface",true,"The type of interface:console or swing");
		options.addOption("m","map",true,"File with the descriptions of the city");
		
		try{
			CommandLineParser parser = new BasicParser();
			CommandLine cmdLine = parser.parse(options, args);
			
			//Si el comando es el de ayuda
			if (cmdLine.hasOption("h")){
				help(options);
			}
			
			//Si no es el comando de ayuda
			else{
				//busca el mapa
				map = cmdLine.getOptionValue("m");
				if (map!=null){
					//carga el mapa
					InputStream file = new FileInputStream(map);
					fileLoader = new CityLoaderFromTxtFile();
					city = fileLoader.loadCity(file);
					
					//busca que interface es
					String interfaces = cmdLine.getOptionValue("i");
					if (interfaces!=null){
							
						//si es interface grafica
						if (interfaces.equalsIgnoreCase("swing")){
							RobotEngine robot = new RobotEngine(city,
										fileLoader.getInitialPlace(), Direction.NORTH);
							final MainWindow gameWindow = new MainWindow(robot);
							EventQueue.invokeLater(new Runnable() {
							public void run() {
								gameWindow.pack(); 
								gameWindow.setLocationRelativeTo(null);
								gameWindow.setVisible(true);
							}
							
						});
						//si es por consola
						} else if (interfaces.equalsIgnoreCase("console")){
								RobotEngine robot = new RobotEngine(city,
										fileLoader.getInitialPlace(), Direction.NORTH);
								robot.startEngine();
						//si falla algo muestra la ayuda por pantalla
						} else {help(options);}	
					} else {help(options);}	
				} else {help(options);}
			}
			
		} catch (org.apache.commons.cli.ParseException ex){
			System.out.println(ex.getMessage());
			help(options);
		} catch (java.lang.NumberFormatException ex){
			help(options);
		} catch (FileNotFoundException e) {
			System.err
			.println("Error reading the map file: " + map + " (File not found)");
			System.exit(2);
		} catch (IOException e) {
			System.err.println("Format error");
			System.exit(2);
		}
		
	}
	
	/**
	 * command help
	 * @param options
	 */
	private static void help (Options options){
		new HelpFormatter().printHelp(Main.class.getCanonicalName() + 
				  " [-h] [-i <type>] [-m <mapfile>]", options);
	}
	
}
